package com.example.Film.reviews.website.controllers;

import com.example.Film.reviews.website.dao.Box_OfficeRepository;
import com.example.Film.reviews.website.dao.MovieRepository;
import com.example.Film.reviews.website.entities.Box_Office;
import com.example.Film.reviews.website.entities.Movie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("box_offices")
public class Box_OfficeController {

    @Autowired
    Box_OfficeRepository box_officeRepository;

    @Autowired
    MovieRepository movieRepository;

    @GetMapping
    public String displayBox_Office(Model model){

        List<Movie> movies = movieRepository.findAll();
        model.addAttribute("movies",movies);

        List<Box_Office> box_offices =box_officeRepository.findAll();
        model.addAttribute("box_offices",box_offices);
        return "box_office/list-box_office";
    }

    @GetMapping("/new")
    public String displayBox_OfficeForm(Model model){

        List<Movie> movies= movieRepository.findAll();
        model.addAttribute("allMovies",movies);

        Movie aMovie = new Movie();
        model.addAttribute("movie",aMovie);

        Box_Office aBox_Office = new Box_Office();
        model.addAttribute("box_office",aBox_Office);
        return "box_office/new-box_office";
    }

    @PostMapping("/save")
    public String createBox_Office(Model model,@RequestParam List<Long> movie,Box_Office box_office){

        box_officeRepository.save(box_office);
        return "redirect:/box_offices";
    }


}
